<%
  containerId = 'project-file-widget' + widget.id.to_s
  colorMetric = widget_properties['colorMetric']
  sizeMetric = widget_properties['sizeMetric']
  chartTitle = widget_properties['chartTitle']
  maxItems = widget_properties['maxItems'].to_i
%>

<div class="word-cloud-widget" id="<%= containerId %>">
  <!--[if lte IE 8 ]> <h3><%= message('widget.unsupported_browser_warning') -%></h3> <![endif]-->

  <!--[if (gte IE 9)|!(IE)]><!-->
  <% if chartTitle %>
    <h3 style="margin-bottom: 5px;"><%= h(chartTitle) -%></h3>
  <% end %>
  <!--<![endif]-->
</div>

<!--[if (gte IE 9)|!(IE)]><!-->
<script>
  (function () {
    <%
      filter = MeasureFilter.new
      # Test files should not be returned
      qualifiers = Api::Utils.java_facade.getResourceLeavesQualifiers(@project.qualifier).to_a.reject {|q| q == 'UTS'}
      filter.criteria = {:qualifiers => qualifiers, :base => @project.key, :pageSize => maxItems, :sort => 'metric:' + sizeMetric.name, :asc => false}
      filter.metrics = [colorMetric.name, sizeMetric.name]
      filter.execute(self, :user => current_user)
    %>

    var data = {
          metrics: {
            <%= colorMetric.name -%>: {
              name: '<%= colorMetric.short_name -%>',
              direction: <%= colorMetric.direction -%>
            },
            <%= sizeMetric.name -%>: {
              name: '<%= sizeMetric.short_name -%>'
            }
          },
          components: [
            <%
            filter.rows.each  do |row|
              color = row.measure(colorMetric)
              size = row.measure(sizeMetric)
            %>
            {
              key: '<%= escape_javascript row.resource.key -%>',
              name: '<%= escape_javascript row.resource.name -%>',
              longName: '<%= escape_javascript row.resource.long_name -%>',
              qualifier: '<%= escape_javascript row.resource.qualifier -%>',
              measures: {
                <%= colorMetric.name -%>: { val: <%= color ? color.value : "null" -%>, fval: '<%= color ? color.formatted_value : "-" -%>' },
                <%= sizeMetric.name -%>: { val: <%= size ? size.value : "null" -%>, fval: '<%= size ? size.formatted_value : "-" -%>' }
              }
            },
            <% end %>
          ]
        },
        widget = new SonarWidgets.WordCloud();

    widget
        .metrics(data.metrics)
        .metricsPriority(['<%= colorMetric.name -%>', '<%= sizeMetric.name -%>'])
        .components(data.components)
        .options({
          baseUrl: baseUrl + '/dashboard/index/'
        })
        .render('#<%= containerId -%>');

    autoResize(500, function() {
      widget.update('#<%= containerId -%>');
    });
  })();
</script>
<!--<![endif]-->
